package com.turbo.cloud.bpm.web.logic;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.turbo.cloud.bpm.web.entity.CustomFlwGroup;
import com.turbo.cloud.bpm.web.entity.CustomFlwInfo;
import com.turbo.cloud.bpm.web.model.custom.CustomFlwGroupDo;
import com.turbo.cloud.bpm.web.model.custom.CustomFlwGroupQo;
import com.turbo.cloud.bpm.web.model.custom.CustomFlwGroupVo;
import com.turbo.cloud.bpm.web.service.ICustomFlwGroupService;
import com.turbo.cloud.bpm.web.service.ICustomFlwInfoService;
import com.turbo.cloud.core.base.PageQuery;
import com.turbo.cloud.core.constants.CommConstants;
import com.turbo.cloud.core.exception.BusinessException;
import com.turbo.cloud.core.utils.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * <p>
 * 工作流组表 逻辑处理类
 * </p>
 *
 * @author zhangluning
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class FlwGroupLogic {

    final ICustomFlwGroupService customFlwGroupService;
    final ICustomFlwInfoService customFlwInfoService;

    /**
     * 查询流程组分页列表
     *
     * @param pageQuery 分页查询参数
     * @return Page
     */
    public Page<CustomFlwGroupVo> queryFlwGroupPage(PageQuery<CustomFlwGroupQo> pageQuery) {
        CustomFlwGroupQo customFlwGroupQo = PageUtil.getPageQueryParam(pageQuery, CustomFlwGroupQo::new);

        LambdaQueryWrapper<CustomFlwGroup> flwGroupLQW = new LambdaQueryWrapper<>();

        flwGroupLQW.eq(Objects.nonNull(customFlwGroupQo.getStateId()), CustomFlwGroup::getStateId, customFlwGroupQo.getStateId());
        // 模糊查询 组名称和组编码
        flwGroupLQW.and(StrUtil.isNotEmpty(customFlwGroupQo.getGroupNameByLike()), i -> i.like(CustomFlwGroup::getGroupName,
                        customFlwGroupQo.getGroupNameByLike())
                .or()
                .like(StrUtil.isNotEmpty(customFlwGroupQo.getGroupNameByLike()), CustomFlwGroup::getGroupCode,
                        customFlwGroupQo.getGroupNameByLike()));

        flwGroupLQW.orderByDesc(CustomFlwGroup::getCreatedBy);
        Page<CustomFlwGroup> flwGroupPage = customFlwGroupService.page(pageQuery.toPage(), flwGroupLQW);

        return PageUtil.toPageView(flwGroupPage, CustomFlwGroupVo::new);
    }

    /**
     * 新增流程组
     *
     * @param customFlwGroupDo 实体
     */
    public Boolean createFlwGroup(CustomFlwGroupDo customFlwGroupDo) {
        CustomFlwGroup customFlwGroup = new CustomFlwGroup();
        BeanUtils.copyProperties(customFlwGroupDo, customFlwGroup);

        boolean result = customFlwGroupService.save(customFlwGroup);
        if (!result) {
            throw new BusinessException("新增流程组失败");
        }
        return true;
    }

    /**
     * 修改流程组
     *
     * @param customFlwGroupDo 实体
     */
    public Boolean modifyFlwGroup(CustomFlwGroupDo customFlwGroupDo) {
        CustomFlwGroup umsFlwGroup = new CustomFlwGroup();
        BeanUtils.copyProperties(customFlwGroupDo, umsFlwGroup);

        boolean result = customFlwGroupService.updateById(umsFlwGroup);
        if (!result) {
            throw new BusinessException("修改流程组失败");
        }
        return true;
    }

    /**
     * 删除流程组
     *
     * @param id 流程组ID
     */
    public boolean removeFlwGroupById(String id) {
        if (StrUtil.isBlank(id)) {
            throw new BusinessException("流程组ID不能为空");
        }

        LambdaQueryWrapper<CustomFlwInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomFlwInfo::getId, id);
        long count = customFlwInfoService.count(lambdaQueryWrapper);

        if (count > 0) {
            throw new BusinessException("流程组有关联的流程信息，请先删除流程信息");
        }

        boolean result = customFlwGroupService.removeById(id);
        if (!result) {
            throw new BusinessException("删除流程组失败");
        }
        return true;
    }

    /**
     * 开启、关闭流程组有效状态
     *
     * @param id 流程组ID
     */
    public Boolean enabledFlwGroupStateById(String id) {
        CustomFlwGroup customFlwGroup = customFlwGroupService.getById(id);
        if (Objects.isNull(customFlwGroup)) {
            throw new BusinessException("流程组不存在");
        }

        if (CommConstants.DICT_STATE_ID_YES.equals(customFlwGroup.getStateId())) {
            customFlwGroup.setStateId(0);
        } else {
            customFlwGroup.setStateId(1);
        }

        boolean result = customFlwGroupService.updateById(customFlwGroup);
        if (!result) {
            throw new BusinessException("修改流程组状态失败");
        }
        return true;
    }
}
